<?php
/**
 * Created by PhpStorm.
 * User: yuyue
 * Date: 13-12-12
 * Time: 上午11:29
 * descript: 此类主要定义一些系统常量，使用时，可以直接  类名::常量名
 */
class Constant{
    // 定义常量
    const title = "爱博123 | 体育彩票，足球竞彩, 赔率信息, 赛前分析";
    const description = "爱博123 体育彩票分析，竞彩推荐，博彩新闻，前瞻和赔率分析推荐。ibo123.com为玩家提供专业竞彩新闻，赛前推荐。获取免费竞彩金，玩金和免费开户红利。";
    const keywords = "足彩，彩票，竞彩，体育博彩新闻, 赔率,  在线博彩公司, 开户红利, 最新赔率, 免费彩金, 玩金，投注金，即时赔率";
    //const只能用于定义简单类型的常量，复杂类型的可以使用static关键字
    public static  $seoarray = array("title"=>self::title,"description"=> self::description,"keywords"=>self::keywords);
    //得到导航条的html,传到当前导航条的cagegoryid,categoryid为父一级，如果是首页，可以不传
    public static function getNav($categoryid=NULL){
        $file_name = PROJECT."/model/cache/nav";
        $handle = fopen($file_name, "r");
        $contents = fread($handle, filesize($file_name));
        fclose($handle);
        $array = unserialize($contents);

        //nav html class="navactive"
        $selectstype = " class=\"navactive\" ";
        $navhtml = "<li ";
        if(empty($categoryid)){
          $navhtml .= $selectstype ;
         }
         $navhtml .= "><a href=\"".WEBSITE_URL."index.html\">首页</a></li>";

        foreach($array as $pcategory){
            $navhtml .="<li";
            if($categoryid == $pcategory['parent']->category_id){
                $navhtml .= $selectstype ;
            }else{
                //还要判断是不是子一级的
                if(!empty($pcategory['childres'])){
                    foreach($pcategory['childres'] as $category){
                        if($categoryid == $category->category_id){
                            $navhtml .= $selectstype ;
                        }
                    }
                }
            }
            $navhtml .= "  ><a href=\"".WEBSITE_URL.$pcategory['parent']->category_id."/index.html\"  >".$pcategory['parent']->category_name."</a>";

            //生成子类
            if(!empty($pcategory['childres'])){

                $navhtml .=" <div class=\"subnav\">  <ul>";
                foreach($pcategory['childres'] as $category){
                    $navhtml .= "<li><a href=\"".WEBSITE_URL.$pcategory['parent']->category_id."/".$category->category_id."/index.html\">$category->category_name</a></li>";
                }
                $navhtml .="  </ul></div>";
            }
            $navhtml .=" </li>";

        }
        $navhtml.="<li><a href=\"".WEBSITE_URL."game\">游戏</a></li><li><a href=\"".WEBSITE_URL."bbs\" target=\"_blank\">爱博说说</a></li>";
        return $navhtml;
    }
   public static  function cutArticle($data,$cut=0,$str="....")
    {

        $data=strip_tags($data);//去除html标记
        $pattern = "/&[a-zA-Z]+;/";//去除特殊符号
        $data=preg_replace($pattern,'',$data);
        if(!is_numeric($cut))
            return $data;
        if($cut>0)
            $data=mb_strimwidth($data,0,$cut,$str);


        return $data;
    }

    /**
     * 网站统计代码
     */
    public static function getTongji(){
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService(self::getDB());
        $sqlParamSql = "select id,name,value from tb_system_param  where name='tongji'";
        $params = $commonService->query($sqlParamSql);
        return $params[0]->value;
    }
    public static function getHotTag(){
        $file_name = PROJECT."/model/cache/hottag";
        $handle = fopen($file_name, "r");
        $contents = fread($handle, filesize($file_name));
        fclose($handle);
        $array = unserialize($contents);
        return $array;
    }
    public static function getHotCate(){
        $file_name = PROJECT."/model/cache/hotcate";
        $handle = fopen($file_name, "r");
        $contents = fread($handle, filesize($file_name));
        fclose($handle);
        $array = unserialize($contents);
        return $array;
    }
    public static function getDB(){
        global  $CONFIG;
        $dbutil = new DbUtil($CONFIG['DB']['db_host'], $CONFIG['DB']['db_user'], $CONFIG['DB']['db_password'], $CONFIG['DB']['db_database']);
         return $dbutil;
    }
    public static function indexHeader(){
            $js=array(WEBSITE_URL."public/js/jquery.imagesloaded.js",
                WEBSITE_URL."public/js/jquery.wookmark.min.js",
                WEBSITE_URL."public/js/listBox.js",
                WEBSITE_URL."public/js/index.js");

            //要考虑在这里会出现的表达式{website_name}，只有这一个。
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService(self::getDB());
        $sqlParamSql = "select id,name,value from tb_system_param  where name in ('website_name','home_title','home_keywords','home_desc')  order by id";
        $params = $commonService->query($sqlParamSql);
        $title = str_replace("{website_name}",$params[0]->value,$params[1]->value);
        $seoarray = array("title"=>$title,"description"=> $params[3]->value,"keywords"=>$params[2]->value,"tongji"=>self::getTongji());
        $pagesets = array("seo"=>$seoarray,"js"=>$js,"nav"=>self::getNav());
         return $pagesets;

    }
    public static function categoryHeader($category){
        require_once COMMON . DS  . 'Constant.class.php';
        $js=array(WEBSITE_URL."public/js/jquery.imagesloaded.js",
            WEBSITE_URL."public/js/jquery.wookmark.min.js",
            WEBSITE_URL."public/js/listBox.js",
            WEBSITE_URL."public/js/category.js");
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService(self::getDB());
        $sqlParamSql = "select id,name,value from tb_system_param  where name in ('website_name','home_title','home_keywords','home_desc','category_title','category_keywords','category_desc')";
        $params = $commonService->query($sqlParamSql);
        $paramkv = array();
        for($i=0;$i<count($params);$i++){
            $paramkv[$params[$i]->name] = $params[$i]->value;
        }
        //hand title
        $title = str_replace("{home_title}",$paramkv['home_title'],$paramkv['category_title']);
        $title = str_replace("{category}",$category->category_name,$title);
        $title = str_replace("{website_name}",$paramkv['website_name'],$title);
        //hand keywords
        $keywords =  str_replace("{home_keywords}",$paramkv['home_keywords'],$paramkv['category_keywords']);
        $keywords = str_replace("{category}",$category->category_name,$keywords);
        //hand desc
        $desc = str_replace("{home_desc}",$paramkv['home_desc'],$paramkv['category_desc']);
        $desc = str_replace("{category}",$category->category_name,$desc);

        $seoarray = array("title"=>$title,"description"=>$desc,"keywords"=>$keywords,"tongji"=>self::getTongji());
        $pagesets = array("seo"=>$seoarray,"js"=>$js,"nav"=>self::getNav($category->category_id));
        return $pagesets;
    }
    public static function postHeader($postId){
        $js = array (
            WEBSITE_URL . "public/assets/js/jquery.cookie.js",
            WEBSITE_URL . "public/js/posts/post.js"
        );

        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService(self::getDB());
        $sqlParamSql = "select id,name,value from tb_system_param  where name in ('website_name','home_title','home_keywords','home_desc','post_title','post_keywords','post_desc')";
        $params = $commonService->query($sqlParamSql);
        $paramkv = array();
        for($i=0;$i<count($params);$i++){
            $paramkv[$params[$i]->name] = $params[$i]->value;
        }
        $postsql = "select * from posts where ID=$postId";
        $posts = $commonService->query($postsql);
        $post = $posts[0];
        //hand title
        $title = str_replace("{home_title}",$paramkv['home_title'],$paramkv['post_title']);
        $title = str_replace("{post_title}",$post->post_title,$title);
        $title = str_replace("{website_name}",$paramkv['website_name'],$title);
        //hand keywords
        $keywords =  str_replace("{home_keywords}",$paramkv['home_keywords'],$paramkv['post_keywords']);
        $keywords =  str_replace("{post_tags}",$post->tags,$keywords);

        //hand desc
        $desc = str_replace("{home_desc}",$paramkv['home_desc'],$paramkv['post_desc']);
        $desc = str_replace("{post_excerpt}",$post->post_excerpt,$desc);
        $seoarray = array("title"=>$title,"description"=>$desc,"keywords"=>$keywords,"tongji"=>self::getTongji());
        $pagesets = array (
            "seo" => $seoarray,
            "js" => $js,
            "nav" => self::getNav ()
        );
        return $pagesets;
    }
    public static function tagHeader($tagname){
        $js=array(WEBSITE_URL."public/js/jquery.imagesloaded.js",
            WEBSITE_URL."public/js/jquery.wookmark.min.js",
            WEBSITE_URL."public/js/listBox.js",
            WEBSITE_URL."public/js/tag.js");
        require_once SERVICE . DS . 'admin/CommonService.class.php';

        $commonService = new CommonService(self::getDB());
        $sqlParamSql = "select id,name,value from tb_system_param  where name in ('website_name','home_title','home_keywords','home_desc','tag_title','tag_keywords','tag_desc')";
        $params = $commonService->query($sqlParamSql);
        $paramkv = array();
        for($i=0;$i<count($params);$i++){
            $paramkv[$params[$i]->name] = $params[$i]->value;
        }
        //hand title
        $title = str_replace("{home_title}",$paramkv['home_title'],$paramkv['tag_title']);
        $title = str_replace("{tag}",$tagname,$title);
        $title = str_replace("{website_name}",$paramkv['website_name'],$title);
        //hand keywords
        $keywords =  str_replace("{home_keywords}",$paramkv['home_keywords'],$paramkv['tag_keywords']);
        $keywords = str_replace("{tag}",$tagname,$keywords);
        //hand desc
        $desc = str_replace("{home_desc}",$paramkv['home_desc'],$paramkv['tag_desc']);
        $desc = str_replace("{tag}",$tagname,$desc);

        $seoarray = array("title"=>$title,"description"=>$desc,"keywords"=>$keywords,"tongji"=>self::getTongji());
        $pagesets = array("seo"=>$seoarray,"js"=>$js,"nav"=>Constant::getNav());

        return $pagesets;
    }
    //得到页面的二级导航 ，也就当前在哪里，传入一个分类id
    public static function getSecCategoryNav($categoryid){
        $file_name = PROJECT."/model/cache/nav";
        $handle = fopen($file_name, "r");
        $contents = fread($handle, filesize($file_name));
        fclose($handle);
        $array = unserialize($contents);
        $sec_cateogory_nav='&gt;';
        foreach($array as $pcategory){
            if($categoryid == $pcategory['parent']->category_id){
                $sec_cateogory_nav .= '<a href='.$categoryid.'/index.html>  '.$pcategory['parent']->category_name.'</a>';
            }else{
                //还要判断是不是子一级的
                if(!empty($pcategory['childres'])){
                    foreach($pcategory['childres'] as $category){
                        if($categoryid == $category->category_id){
                            $sec_cateogory_nav .= '<a href='.$pcategory['parent']->category_id.'/index.html>  '.$pcategory['parent']->category_name.'</a>&gt;';
                            $sec_cateogory_nav .= '<a href='.$pcategory['parent']->category_id.'/'.$categoryid.'/index.html>  '.$category->category_name.'</a>';

                        }
                    }
                }
            }
        }
        return $sec_cateogory_nav;
    }

    //得到页面的二级导航 ，也就当前在哪里，传入一个分类id
    public static function getCategoryName($categoryid){
        $file_name = PROJECT."/model/cache/nav";
        $handle = fopen($file_name, "r");
        $contents = fread($handle, filesize($file_name));
        fclose($handle);
        $array = unserialize($contents);
        $categoryname= '';

        foreach($array as $pcategory){
            if($categoryid == $pcategory['parent']->category_id){
                $categoryname =  $pcategory['parent']->category_name;
            }else{
                //还要判断是不是子一级的
                if(!empty($pcategory['childres'])){
                    foreach($pcategory['childres'] as $category){
                        if($categoryid == $category->category_id){
                            $categoryname =  $category->category_name;
                            break;
                        }
                    }
                }
            }
        }
        return $categoryname;
    }
    //得到页面的二级导航 ，也就当前在哪里，传入一个分类id
    public static function getCategorylist(){
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService(self::getDB());
        $sqlParamSql = "SELECT DISTINCT(category_name) FROM post_category WHERE parent = '1'";
        $params = $commonService->query($sqlParamSql);
        $paramkv = array();
        for($i=0;$i<count($params);$i++){
            $paramkv[] = array(
                "name" => $params[$i]->category_name,
                "url" => WEBSITE_URL."blog".DS."index?type=".$params[$i]->category_name
            );
        }
        return $paramkv;
    }
}